﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FrameWork.Lib.Net
{
    public static class Functions
    {
        public static string GetSource(string URL)
        {
            return GetSource(URL, Encoding.Default);
            //try
            //{
            //    return GetSourceEx(URL);
            //}
            //catch (Exception exception)
            //{
            //    System.Diagnostics.Debug.WriteLine(exception.ToString());
            //    for (int i = 0; i <= 5; i++)
            //    {
            //        try
            //        {
            //            return GetSourceEx(URL);
            //        }
            //        catch (Exception exception2)
            //        {
            //            System.Diagnostics.Debug.WriteLine("retry(" + i.ToString() + ") " + exception2.ToString());
            //        }
            //    }
            //}
            //return null;
        }

        //private static string GetSourceEx(string pURL)
        //{
        //    var tWebRequest = System.Net.WebRequest.Create(FormatURL(pURL));
        //    var tResponse = tWebRequest.GetResponse();
        //    var tResponseStream = tResponse.GetResponseStream();
        //    var tResult = "";

        //    using (var sr = new System.IO.StreamReader(tResponseStream))
        //    {
        //        tResult = sr.ReadToEnd();
        //    }

        //    return tResult;
        //}

        public static string FormatURL(string URL)
        {
            return URL.Replace("//", "/").Replace("http:/", "http://");
        }

        public static string GetSource(string pURL, System.Text.Encoding pEncoding)
        {
            string tResult = "";

            for (int i = 0; i <= 5; i++)
            {
                try
                {
                    var tRequest = System.Net.HttpWebRequest.Create(pURL);

                    using (var tResponse = tRequest.GetResponse())
                    {
                        using (var sr = new System.IO.StreamReader(tResponse.GetResponseStream(), pEncoding))
                        {
                            tResult = sr.ReadToEnd();
                        }
                    }


                    break;
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("retry(" + i.ToString() + ") " + ex.ToString());
                }
            }
            return tResult;
        }


        public static string DownloadFile(string pSourceLink, string pDestFileName)
        {
            string tMessage = "";

            var wc = new System.Net.WebClient();
            for (int i = 0; i < 3; i++)
            {
                try
                {
                    tMessage = "";

                    if (!System.IO.File.Exists(string.Format(@".\Completed\{0}", pDestFileName)))
                    {
                        wc.DownloadFile(pSourceLink, string.Format(@".\downloading\{0}", pDestFileName));

                        System.IO.File.Move(string.Format(@".\downloading\{0}", pDestFileName), string.Format(@".\Completed\{0}", pDestFileName));

                        break;
                    }
                    else
                    {
                        break;
                    }

                }
                catch (Exception ex)
                {
                    tMessage = ex.Message;

                    if (System.IO.File.Exists(string.Format(@".\downloading\{0}", pDestFileName)))
                        System.IO.File.Delete(string.Format(@".\downloading\{0}", pDestFileName));
                }
            }

            if (tMessage.Length > 0 && System.IO.File.Exists(string.Format(@".\downloading\{0}", pDestFileName)))
                System.IO.File.Delete(string.Format(@".\downloading\{0}", pDestFileName));

            return tMessage;

        }

    }
}
